﻿using Loyalty.Core.Entities;
using Loyalty.Core.Entities.Mapping;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Loyalty.Application
{
    
    public class LoyaltyContext: DbContext,IUnitOfWork
    {

        public DbSet<City> Cities { get; set; }
        public DbSet<Grade> Grades { get; set; }
        public DbSet<JobType> JobTypes { get; set; }
        public DbSet<Profile> Profiles { get; set; }
        public DbSet<ProfileGrade> ProfileGrades { get; set; }
        public DbSet<ProfileJob> ProfileJobs { get; set; }
        public DbSet<Province> Provinces { get; set; }
        public DbSet<Skill> Skills { get; set; }
        public DbSet<UniversityCourse> UniversityCourses { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new CityMap());
            modelBuilder.Configurations.Add(new GradeMap());
            modelBuilder.Configurations.Add(new JobTypeMap());
            modelBuilder.Configurations.Add(new ProfileMap());
            modelBuilder.Configurations.Add(new ProfileGradeMap());
            modelBuilder.Configurations.Add(new ProfileJobMap());
            modelBuilder.Configurations.Add(new ProvinceMap());
            modelBuilder.Configurations.Add(new SkillMap());
            modelBuilder.Configurations.Add(new UniversityCourseMap());

            base.OnModelCreating(modelBuilder);
        }

        protected override void Dispose(bool disposing)
        {
            this.Configuration.LazyLoadingEnabled = false;
            base.Dispose(disposing);
        }

        public LoyaltyContext(): base("LoyaltyCnn")
        {
            this.Configuration.LazyLoadingEnabled = false;
            this.Configuration.AutoDetectChangesEnabled = true;
            this.Configuration.ProxyCreationEnabled = false;
            this.Configuration.ValidateOnSaveEnabled = true;
            
        }

        public IDbSet<TEntity> DbSet<TEntity>() where TEntity : class
        {
            return base.Set<TEntity>();
        }

        public DbChangeTracker GetChangeTracker()
        {
            return this.ChangeTracker;
        }

        public override int SaveChanges()
        {
            return base.SaveChanges();
        }

        public override Task<int> SaveChangesAsync()
        {
            return base.SaveChangesAsync();
        }
    }
}
